package net.surina.soundtouch;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.util.Log;

import com.audiocap.NativeSoundTouch;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by liqian-ps on 2018/6/15.
 */

public class PlayPcmThread extends Thread {
    public  static final String TAG = PlayPcmThread.class.getName();
    private AudioTrack mAudioTrack;
    private byte[] playerBuffer;

    private boolean recordingstart = false;

    private boolean playingstart = false;
    private int minRecBuffSize = 0;
    private int minPlayBufferSize = 0;
    private String fileName;
    private FileInputStream playInputStream = null;
    private SonicAudioProcessor  mSonicAudioProcessor;

    public PlayPcmThread(String filename){
        this.fileName = filename;
        try {
            playInputStream = new FileInputStream(new File(this.fileName));

            minPlayBufferSize = AudioTrack.getMinBufferSize(Util.SAMPLERATE,
                    AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

            mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, Util.SAMPLERATE,
                    AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
                    minPlayBufferSize * 3, AudioTrack.MODE_STREAM);

//            mSonicAudioProcessor = new SonicAudioProcessor();
//            mSonicAudioProcessor.configure(44100, 2, C.ENCODING_PCM_16BIT);
//            mSonicAudioProcessor.setPitch(1.0f);
//            mSonicAudioProcessor.flush();
//            NativeSoundTouch.getSoundTouch().setTempoChange(100.0f);
            if (minPlayBufferSize != 0) {
                playerBuffer = new byte[minPlayBufferSize * 3];
                mAudioTrack.play();
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {

        byte[] processbuffer = new byte[minPlayBufferSize * 3];
        int receiveSTSamples = 0;
        while (playingstart && playInputStream != null) {
            int len = 0;
            try {
                len = playInputStream.read(playerBuffer, 0,
                        playerBuffer.length);

//                ByteBuffer out = handlePcm(playerBuffer, len);

                if (len == -1) {
                    // 读完了，重新再来
                    break;
//                    Log.d(TAG, "file read finish!");
//                    try {
//                        break;
////                        playInputStream.close();
////                        playInputStream = new FileInputStream(new File(this.fileName));
//                    } catch (FileNotFoundException e) {
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
                } else if (len != 0) {
                    Log.e(TAG, "before shiftingPitch len" + len);
//                    NativeSoundTouch.getSoundTouch().shiftingPitch(
//                            playerBuffer, 0, len);
                    Log.e(TAG, "end shiftingPitch len" + len);

                    do {
                        Log.e(TAG, "receiveSamples");
//                        receiveSTSamples = NativeSoundTouch.getSoundTouch().receiveSamples(processbuffer, processbuffer.length);
                        Log.e(TAG, "mAudioTrack.write");
                        mAudioTrack.write(processbuffer, 0, receiveSTSamples * 2);
                    }while (receiveSTSamples != 0);


                }

            } catch (IOException e) {

            }
        }

        mAudioTrack.stop();
        mAudioTrack.release();
        playingstart = false;

        if (playInputStream != null) {
            try {
                playInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setStart(boolean stop){
        playingstart = stop;
    }

//    private ByteBuffer handlePcm(byte[] data){
//        ByteBuffer byteBuffer = ByteBuffer.wrap(data);
//
//        mSonicAudioProcessor.queueInput(byteBuffer);
//        ByteBuffer output = mSonicAudioProcessor.getOutput();
//        return output;
//    }

    private void handlePcm(byte[] data, int len){
//        NativeSoundTouch.getSoundTouch().shiftingPitch(
//                playerBuffer, 0, len);
    }

}
